package com.vq3.digitalizationsoftwareintegrates.Service.User.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.*;
import com.vq3.digitalizationsoftwareintegrates.Entity.UserManageCenter.UserManageCenterEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.UserManageCenter.UserManageCenterEntity_perEntity;
import com.vq3.digitalizationsoftwareintegrates.Mapper.Spot_Check.Group_TBMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.User.*;
import com.vq3.digitalizationsoftwareintegrates.Mapper.UserManageCenter.UserManageCenterMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.UserManageCenter.UserManageCenter_perMapper;
import com.vq3.digitalizationsoftwareintegrates.Service.User.UserFusionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


@Service
public class UserFusionServiceImpl implements UserFusionService {

    @Resource
    Group_TBMapper groupTbMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    DeleteStatusMapper deleteStatusMapper;
    @Resource
    User_RoleMapper user_roleMapper;
    @Resource
    URole_TBMapper uRole_tbMapper;
    @Resource
    UserManageCenter_perMapper userManageCenter_perMapper;
    @Resource
    UserManageCenterMapper userManageCenterMapper;
    @Resource
    Urole_PermissionMapper urole_permissionMapper;
    @Resource
    Permission_TBMapper permission_tbMapper;
    @Resource
    UserFusionService userFusionService;


    public Group_TBEntity calculationGroupType(Integer groupID) {

        QueryWrapper<Group_TBEntity> group_tbEntityQueryWrapper = new QueryWrapper<>();
        group_tbEntityQueryWrapper.eq("group_id",groupID);
        return groupTbMapper.selectOne(group_tbEntityQueryWrapper);
    }

    public List<Group_TBEntity> findAllGroupType() {

        return groupTbMapper.selectList(null);
    }

    public IPage<UserEntity> getAllUserData(int page) {
        IPage<UserEntity> userIPage = new Page<>(page,10);
        return userMapper.selectPage(userIPage,null);
    }

    @Override
    public List<DeleteStatusEntity> getUserDeleteStatus() {
        return deleteStatusMapper.selectList(null);
    }

    @Override
    public boolean setUserDeleteStatus(String uname, String userDeleteStatusValue) {

        UpdateWrapper<UserEntity> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("uname",uname).set("deletestatus",userDeleteStatusValue);
        int update = userMapper.update(new UserEntity(), userUpdateWrapper);
        return update > 0;
    }

    @Override
    public boolean setUserGroup(String uname, Integer group) {
        UpdateWrapper<UserEntity> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("uname",uname).set("group_id",group);
        int update = userMapper.update(new UserEntity(), userUpdateWrapper);
        return update > 0;
    }

    @Override
    public Long uidFindURoleCount(Integer uid) {
        QueryWrapper<User_RoleEntity> userRoleEntityQueryWrapper = new QueryWrapper<>();
        userRoleEntityQueryWrapper.eq("user_id",uid);
        return user_roleMapper.selectCount(userRoleEntityQueryWrapper);
    }

    @Override
    public List<User_RoleEntity> uidFindURole(Integer uid, int page) {
        int start = ((page -1 ) * 10);
        int size = 10;
        return user_roleMapper.uidFindURole(uid,start,size);

    }

    @Override
    public List<User_RoleEntity> uidFindURole(Integer uid) {
        QueryWrapper<User_RoleEntity> userRoleEntityQueryWrapper = new QueryWrapper<>();
        userRoleEntityQueryWrapper.eq("user_id",uid);
        return user_roleMapper.selectList(userRoleEntityQueryWrapper);

    }

    @Override
    public URole_TBEntity uRidFindUR(Integer uroleId) {
        QueryWrapper<URole_TBEntity> uRole_tbEntityQueryWrapper = new QueryWrapper<>();
        uRole_tbEntityQueryWrapper.eq("urole_id",uroleId);
        return uRole_tbMapper.selectOne(uRole_tbEntityQueryWrapper);

    }

    @Override
    public IPage<URole_TBEntity> findAllUserRole(int page, int i) {
        IPage<URole_TBEntity> uRole_tbEntityIPage = new Page<>(page,i);
        return uRole_tbMapper.selectPage(uRole_tbEntityIPage,null);
    }

    @Override
    public List<URole_TBEntity> findAllUserRole() {
        return uRole_tbMapper.selectList(null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean allocationRole(LinkedHashMap<Integer, Integer> hashMap, Integer uid) {
        QueryWrapper<User_RoleEntity> userRoleEntityQueryWrapper = new QueryWrapper<>();
        userRoleEntityQueryWrapper.eq("user_id",uid);
        try {
            user_roleMapper.delete(userRoleEntityQueryWrapper);
        }catch (Exception e){
            e.printStackTrace();
        }

        boolean flag = false;
        for (Map.Entry<Integer, Integer> integerIntegerEntry : hashMap.entrySet()) {
            if (integerIntegerEntry.getValue() == 2) {
                User_RoleEntity user_roleEntity = new User_RoleEntity();
                user_roleEntity.setUserId(uid);
                user_roleEntity.setUroleId(integerIntegerEntry.getKey());
                int insert = user_roleMapper.insert(user_roleEntity);
                if (insert < 1) {
                    return false;
                }else {
                    flag = true;
                }
            }
        }
        /*user_roleMapper.delete(userRoleEntityQueryWrapper);*/
        return flag;
    }

    @Override
    public List<UserManageCenterEntity> findUserManageCenterSideBar(List<User_RoleEntity> user_roleEntities) {
        QueryWrapper<UserManageCenterEntity_perEntity> queryWrapper = new QueryWrapper<>();
        for (User_RoleEntity user_roleEntity : user_roleEntities) {
            queryWrapper.or().eq("umid",user_roleEntity.getUroleId());
        }
        List<UserManageCenterEntity_perEntity> dataCenter_roleEntities = userManageCenter_perMapper.selectList(queryWrapper);

        QueryWrapper<UserManageCenterEntity> dataCenter_roleEntityQueryWrapper = new QueryWrapper<>();
        for (UserManageCenterEntity_perEntity dataCenter_roleEntity_role_tbEntity : dataCenter_roleEntities) {
            dataCenter_roleEntityQueryWrapper.or().eq("id",dataCenter_roleEntity_role_tbEntity.getUmid());
        }
        return userManageCenterMapper.selectList(dataCenter_roleEntityQueryWrapper);



    }

    @Override
    public IPage<URole_TBEntity> getAllRole(int page) {
        IPage<URole_TBEntity> userIPage = new Page<>(page,10);
        return uRole_tbMapper.selectPage(userIPage,null);

    }

    @Override
    public Long uRoleIdFindPermissionId(Integer uRoleId) {
        QueryWrapper<Urole_PermissionEntity> Urole_PermissionEntityQueryWrapper = new QueryWrapper<>();
        Urole_PermissionEntityQueryWrapper.eq("u_p_urole_id",uRoleId);
        return urole_permissionMapper.selectCount(Urole_PermissionEntityQueryWrapper);

    }

    @Override
    public Long uRoleIdFindUserId(Integer uRoleId) {
        QueryWrapper<User_RoleEntity> user_RoleEntityQueryWrapper = new QueryWrapper<>();
        user_RoleEntityQueryWrapper.eq("urole_id",uRoleId);
        return user_roleMapper.selectCount(user_RoleEntityQueryWrapper);
    }

    @Override
    public URole_TBEntity roleFindRid(String role) {
        QueryWrapper<URole_TBEntity> uRole_tbEntityQueryWrapper = new QueryWrapper<>();
        uRole_tbEntityQueryWrapper.eq("alias",role);
        return uRole_tbMapper.selectOne(uRole_tbEntityQueryWrapper);

    }

    @Override
    public List<User_RoleEntity> ridFindURList(URole_TBEntity rid,int page) {
        int start = ((page -1 ) * 10);
        int size = 10;
        return user_roleMapper.ridFindUser(rid.getUroleId(),start,size);
    }

    @Override
    public IPage<Permission_TBEntity> findAllPermission(int page) {
        IPage<Permission_TBEntity> permission_tbEntityIPage = new Page<>(page,10);
        return permission_tbMapper.selectPage(permission_tbEntityIPage,null);
    }

    @Override
    public List<Urole_PermissionEntity> uRidFindURPermission(Integer uroleId) {
        QueryWrapper<Urole_PermissionEntity> urole_permissionEntityQueryWrapper = new QueryWrapper<>();
        urole_permissionEntityQueryWrapper.eq("u_p_urole_id",uroleId);
        return urole_permissionMapper.selectList(urole_permissionEntityQueryWrapper);
    }

    @Override
    public boolean setRoleDeleteUser(Integer uid, Integer uroleId) {
        QueryWrapper<User_RoleEntity> userRoleEntityQueryWrapper = new QueryWrapper<>();
        userRoleEntityQueryWrapper.eq("user_id",uid).eq("urole_id",uroleId);
        int delete = user_roleMapper.delete(userRoleEntityQueryWrapper);
        return delete > 0;
    }

    @Override
    public Permission_TBEntity permissionFindpermissionId(String permission) {
        QueryWrapper<Permission_TBEntity> permission_tbEntityQueryWrapper = new QueryWrapper<>();
        permission_tbEntityQueryWrapper.eq("permission_context",permission);
        return permission_tbMapper.selectOne(permission_tbEntityQueryWrapper);

    }

    @Override
    public void setDeleteRolePermissionBtn(Integer uroleId, List<String> authArr, List<String> arr) {


        for (String s : arr) {
            QueryWrapper<Urole_PermissionEntity> urole_permissionEntityQueryWrapper = new QueryWrapper<>();
            String permission = s.replace("\"", "").replace("[", "").replace("]", "");
            Permission_TBEntity permission_tbEntity = userFusionService.permissionFindpermissionId(permission);
            urole_permissionEntityQueryWrapper.eq("u_p_urole_id",uroleId).eq("u_p_permission_id",permission_tbEntity.getPermissionId());


            int delete = urole_permissionMapper.delete(urole_permissionEntityQueryWrapper);
            System.out.println(delete);
        }


            for (String s : authArr) {

            String permission = s.replace("\"", "").replace("[", "").replace("]", "");
                System.out.println(permission);
            if (!permission.equalsIgnoreCase("")){
                Permission_TBEntity permission_tbEntity = userFusionService.permissionFindpermissionId(permission);
                Urole_PermissionEntity urole_permissionEntity = new Urole_PermissionEntity();
                urole_permissionEntity.setUPUroleId(uroleId);
                urole_permissionEntity.setUPPermissionId(permission_tbEntity.getPermissionId());
                urole_permissionMapper.insert(urole_permissionEntity);
            }
        }



    }

    @Override
    public Group_TBEntity groupIdFindGroup(Integer groupId) {
        QueryWrapper<Group_TBEntity> group_tbEntityQueryWrapper = new QueryWrapper<>();
        group_tbEntityQueryWrapper.eq("group_id",groupId);
        return groupTbMapper.selectOne(group_tbEntityQueryWrapper);
    }

    @Override
    public Permission_TBEntity permissionIdFindpermission(Integer upPermissionId) {
        QueryWrapper<Permission_TBEntity> permission_tbEntityQueryWrapper = new QueryWrapper<>();
        permission_tbEntityQueryWrapper.eq("permission_id",upPermissionId);
        return permission_tbMapper.selectOne(permission_tbEntityQueryWrapper);
    }

    @Override
    public List<Urole_PermissionEntity> permissionIdFindURoleId(Integer permissionId) {
        QueryWrapper<Urole_PermissionEntity> urole_permissionEntityQueryWrapper = new QueryWrapper<>();
        urole_permissionEntityQueryWrapper.eq("u_p_permission_id",permissionId);
        return urole_permissionMapper.selectList(urole_permissionEntityQueryWrapper);

    }

    @Override
    public List<Permission_TBEntity> uidFindPermissionTb(Integer uid) {
        return null;
    }

    @Override
    public List<UserManageCenterEntity_perEntity> perFindUMId(Permission_TBEntity permission_tbEntity) {
        QueryWrapper<UserManageCenterEntity_perEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("per",permission_tbEntity.getPermission());
        return userManageCenter_perMapper.selectList(queryWrapper);
    }

    @Override
    public List<UserManageCenterEntity> umidFindUMSideBar(Set<Integer> um_idS) {
        List<UserManageCenterEntity> userManageCenterEntities = new ArrayList<>();
        for (Integer um_id : um_idS) {
            QueryWrapper<UserManageCenterEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("umid",um_id);
            UserManageCenterEntity userManageCenterEntity = userManageCenterMapper.selectOne(queryWrapper);
            userManageCenterEntities.add(userManageCenterEntity);
        }
        return userManageCenterEntities;
    }


}
